package DBConnection;

import java.util.List;

import model.entities.StatisticStation;

import org.hibernate.HibernateException;

public class StatisticStationDB extends HibernateUtil implements
		ConnectDB<StatisticStation, Boolean, String> {

	@Override
	public Boolean add(StatisticStation StatisticStation) {
		try {
			if (StatisticStation != null) {
				this.beginTranSaction();
				this.getCurrentSession().save(StatisticStation);
				this.commitTransaction();
			}
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
		return true;
	}

	@Override
	public Boolean delete(String ID) {
		StatisticStation StatisticStation = find(ID);
		try {
			if (StatisticStation != null) {
				this.beginTranSaction();
				this.getCurrentSession().delete(StatisticStation);
				this.commitTransaction();
			}
		} catch (HibernateException e) {
			this.rollback();
			throw e;
		}
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<StatisticStation> findAll() {
		try {
			this.beginTranSaction();
			return this.getCurrentSession()
					.createCriteria(StatisticStation.class).list();
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}

	@Override
	public Boolean update(StatisticStation StatisticStation) {
		try {
			if (StatisticStation != null) {
				this.beginTranSaction();
				this.getCurrentSession().update(StatisticStation);
				this.commitTransaction();
			}
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
		return true;
	}

	@Override
	public StatisticStation find(String ID) {
		try {
			this.beginTranSaction();
			return (StatisticStation) this.getCurrentSession().get(
					StatisticStation.class, ID);
		} catch (Exception e) {
			throw e;
		}
	}
	public static void main(String[] args) {
		List<StatisticStation> ll = new StatisticStationDB().findAll();
		System.out.println(ll.size());
		System.out.println(new StatisticStationDB().find("WS"));
	}

}
